<?php
/**
 * Created by PhpStorm.
 * User: edmmss
 * Date: 2018/12/5
 * Time: 11:11
 */

namespace app\modules\fluentInterface;


class Sql
{
    private $_fields = [];

    private $_form = [];

    private $_where = [];

    public function select(array $fields): Sql
    {
        $this->_fields = $fields;

        return $this;
    }

    public function from(string $table, string $alias = ''): Sql
    {
        if ($alias) {
            $this->_form[] = $table . ' AS ' . $alias;
        } else {
            $this->_form[] = $table;
        }

        return $this;
    }

    public function where(string $condition): Sql
    {
        $this->_where[] = $condition;

        return $this;
    }

    public function __toString(): string
    {
        // TODO: Implement __toString() method.
        return sprintf(
            'SELECT %s FROM %s WHERE %s',
            join(', ', $this->_fields),
            join(', ', $this->_form),
            join(' AND ', $this->_where)
        );
    }
}